﻿import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Window
import FilUI

FilPopup {
    id: control

    property string title                       : ""
    property string message                     : ""
    property string neutralText                 : qsTr("Close")
    property string negativeText                : qsTr("Cancel")
    property string positiveText                : qsTr("OK")
    property int messageTextFormart             : Text.AutoText
    property int delayTime                      : 100
    property int buttonFlags                    : FilContentDialogType.NegativeButton | FilContentDialogType.PositiveButton
    property bool antiShake                     : true
    property var contentDelegate                :  Component {
        Item { }
    }
    property var onNeutralClickListener
    property var onNegativeClickListener
    property var onPositiveClickListener

    signal neutralClicked
    signal negativeClicked
    signal positiveClicked

    implicitWidth                               : 400
    implicitHeight                              : layout_content.height
    focus                                       : true

    QtObject {
        id: d
        property bool enableShake               : false
    }

    onOpened: {
        d.enableShake = true
    }

    Component {
        id: com_message
        Flickable {
            id: sroll_message
            contentHeight                       : text_message.height
            contentWidth                        : width
            clip                                : true
            boundsBehavior                      : Flickable.StopAtBounds
            width                               : parent.width
            height                              : message === "" ? 0 : Math.min(text_message.height, 300)
            ScrollBar.vertical                  : FilScrollBar { }
            FilText {
                id: text_message
                font                            : FilTextStyle.Body
                wrapMode                        : Text.WordWrap
                text                            : message
                width                           : parent.width
                topPadding                      : 4
                leftPadding                     : 20
                rightPadding                    : 20
                bottomPadding                   : 4
            }
        }
    }
    Rectangle {
        id: layout_content
        width                                   : parent.width
        height                                  : layout_column.childrenRect.height
        color                                   : 'transparent'
        radius                                  : 5
        ColumnLayout {
            id: layout_column
            width                               : parent.width
            FilText {
                id: text_title
                font                            : FilTextStyle.Title
                text                            : title
                topPadding                      : 20
                leftPadding                     : 20
                rightPadding                    : 20
                wrapMode                        : Text.WordWrap
            }
            Loader {
                sourceComponent                 : com_message
                Layout.fillWidth                : true
                Layout.preferredHeight          : status===Loader.Ready ? item.height : 0
                Component.onDestruction         : sourceComponent = undefined
            }
            Loader {
                sourceComponent                 : control.visible ? control.contentDelegate : undefined
                Layout.fillWidth                : true
                onStatusChanged                 : {
                    if (status === Loader.Ready) {
                        Layout.preferredHeight = item.implicitHeight
                    } else {
                        Layout.preferredHeight = 0
                    }
                }
                Component.onDestruction: sourceComponent = undefined
            }
            Rectangle {
                id: layout_actions
                Layout.fillWidth                : true
                Layout.preferredHeight          : 60
                radius                          : 5
                color                           : Qt.rgba(243/255, 243/255, 243/255, 1)
                RowLayout {
                    anchors {
                        centerIn                : parent
                        margins                 : spacing
                        fill                    : parent
                    }
                    spacing                     : 10
                    Item {
                        Layout.fillWidth        : true
                        Layout.fillHeight       : true
                        FilButton {
                            id: neutral_btn
                            visible             : control.buttonFlags & FilContentDialogType.NeutralButton
                            text                : neutralText
                            width               : parent.width
                            anchors.centerIn    : parent
                            onClicked           : {
                                if (d.enableShake || !control.antiShake) {
                                    d.enableShake = false
                                    if (control.onNeutralClickListener) {
                                        control.onNeutralClickListener()
                                    } else {
                                        neutralClicked()
                                        control.close()
                                    }
                                }
                            }
                        }
                    }
                    Item {
                        Layout.fillWidth        : true
                        Layout.fillHeight       : true
                        FilButton {
                            id: negative_btn
                            visible             : control.buttonFlags & FilContentDialogType.NegativeButton
                            width               : parent.width
                            anchors.centerIn    : parent
                            text                : negativeText
                            onClicked           : {
                                if (d.enableShake || !control.antiShake) {
                                    d.enableShake = false
                                    if (control.onNegativeClickListener) {
                                        control.onNegativeClickListener()
                                    } else {
                                        negativeClicked()
                                        control.close()
                                    }
                                }
                            }
                        }
                    }
                    Item {
                        Layout.fillWidth        : true
                        Layout.fillHeight       : true
                        FilFilledButton {
                            id: positive_btn
                            visible             : control.buttonFlags & FilContentDialogType.PositiveButton
                            text                : positiveText
                            width               : parent.width
                            anchors.centerIn    : parent
                            onClicked           : {
                                if (d.enableShake || !control.antiShake) {
                                    d.enableShake = false
                                    if (control.onPositiveClickListener) {
                                        control.onPositiveClickListener()
                                    } else {
                                        positiveClicked()
                                        control.close()
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
